<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-form :inline="true" :model="listQuery" ref="dataList2" class="demo-form-inline">
            <el-input class="marginRB10" v-model="listQuery.room_id" placeholder="房间ID" style="width:200px;"
                      clearable></el-input>
            <el-input class="marginRB10" v-model="listQuery.room_name" placeholder="房间名称" style="width:200px;"
                      clearable></el-input>
            <el-input class="marginRB10" v-model="listQuery.user_code" placeholder="房主ID" style="width:200px;"
                      clearable></el-input>
            <el-input class="marginRB10" v-model="listQuery.nickname" placeholder="房主昵称" style="width:200px;"
                      clearable></el-input>
            <el-select filterable v-model="listQuery.state" class="filter-item marginRB10" clearable placeholder='锁房状态'
                       style="width: 200px;">
              <el-option v-for="item in selectList" :key="item.key" :label="item.lab" :value="item.key"></el-option>
            </el-select>
            <el-date-picker
              style="width:200px;"
              v-model="listQuery.lock_time"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择锁房时间">
            </el-date-picker>
            <el-date-picker
              style="width:200px;"
              v-model="listQuery.unlock_time"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择解锁时间">
            </el-date-picker>
            <el-button type="primary" @click="searchList" class="marginRB10">查询</el-button>
            <el-button type="primary" @click="resetList" class="marginRB10">重置</el-button>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table ref="dataList" :data="menuList" style="margin-bottom: 20px;" row-key="id" border
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <!-- 房间id	房间名称	房主ID	房主昵称	房主头像	锁房时间	状态 -->
          <el-table-column prop="room_id" label="房间id"></el-table-column>
          <el-table-column prop="room_name" label="房间名称"></el-table-column>
          <el-table-column prop="user_code" label="房主ID"></el-table-column>
          <el-table-column prop="nickname" label="房主昵称"></el-table-column>
          <el-table-column prop="head_picture" label="房主头像">
            <template slot-scope="scope">
              <el-popover placement="right" max-width="220" trigger="hover">
                <img :src="scope.row.head_picture" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.head_picture" alt="" style="width: 80px;height: 80px">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="lock_time" label="锁房时间"></el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.state === 2">
                <div>
                  <el-tag type="success">已解锁</el-tag>
                </div>
                <div>
                  <p>解锁时间:{{ scope.row.unlock_time }}</p>
                  <p>累积锁房{{ scope.row.lock_delay_time }}秒</p>
                </div>
              </div>
              <el-tag type="danger" v-if="scope.row.state === 1">锁房中</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <cus-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible"
               @close="handleDialogClose">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="120px"
               style="width: 400px; margin: 0 auto;">
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm()">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {saveData, deleteData, updateData, listData} from '@/api/room/lock_log'

export default {
  data() {
    return {
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
        room_name: undefined,
        room_id: undefined,
        user_code: undefined,
        nickname: undefined,
        lock_time: undefined,
        unlock_time: undefined,
        state: undefined,
      },
      selectList: [{key: '1', lab: '锁房中'}, {key: '2', lab: '已解锁'}],
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      form: {
        id: undefined,
        name: undefined,
      },
      rules: {
        name: [{required: true, message: '请输入名字', trigger: 'blur'}],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let urlData = '?';
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`
      }
      if (this.listQuery.room_name) {
        urlData = urlData + `&room_name=${this.listQuery.room_name}`
      }
      if (this.listQuery.room_id) {
        urlData = urlData + `&room_id=${this.listQuery.room_id}`
      }
      if (this.listQuery.user_code) {
        urlData = urlData + `&user_code=${this.listQuery.user_code}`
      }
      if (this.listQuery.state) {
        urlData = urlData + `&state=${this.listQuery.state}`
      }
      if (this.listQuery.lock_time) {
        urlData = urlData + `&lock_time=${this.listQuery.lock_time}`
      }
      if (this.listQuery.unlock_time) {
        urlData = urlData + `&unlock_time=${this.listQuery.unlock_time}`
      }
      if (this.listQuery.nickname) {
        urlData = urlData + `&nickname=${this.listQuery.nickname}`
      }

      listData(urlData).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total
        this.listLoading = false
      })
    },
    resetList() {
      this.listQuery.room_name = undefined;
      this.listQuery.room_id = undefined;
      this.listQuery.user_code = undefined;
      this.listQuery.nickname = undefined;
      this.listQuery.lock_time = undefined;
      this.listQuery.state = undefined;
      this.listQuery.unlock_time = undefined;
    },

    handleCreate(row) {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.form = []
      this.form.id = row.id
      this.form.name = row.name

      this.dialogStatus = 'update'
      this.dialogFormVisible = true

      this.form = Object.assign({}, this.form)
    },
    handleDelete(row) {
      let data = {ids: row.id}
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        }
      })
    },
    allDel() {
      let delNum = []
      this.$refs.dataList.selection.forEach((k) => {
        delNum.push(k.id)
      })
      if (delNum.length === 0) {
        return;
      }
      let data = {ids: delNum.toString()}
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        }
      })
    },
    searchList() {
      this.listQuery.page = 1
      this.getList()
    },
    submitForm() {
      this.$refs['dataForm'].validate(valid => {
        if (valid && this.dialogStatus === 'create') {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined ? '' : formData.append(item, this.form[item]);
          }
          saveData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.message,
                type: 'error'
              });
            }
          })
        } else if (valid && this.dialogStatus === 'update') {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined ? '' : formData.append(item, this.form[item]);
          }
          updateData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.message,
                type: 'error'
              });
            }
          })
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined,
      }
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>
<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
